package com.wlsk.license.controller.common;

import com.wlsk.license.cache.LocalCacheApiOperation;
import com.wlsk.license.cache.LocalCacheContext;
import com.wlsk.license.common.result.Result;
import com.wlsk.license.dto.request.LocalCacheOptReqDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @description: 本地缓存管理
 * @author: liuzhangbin
 * @date: 2025/2/17
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api(tags = "缓存管理")
@RequestMapping("/cache/manage")
public class LocalCacheController {

    @Resource
    private LocalCacheContext localCacheContext;

    @Resource
    private LocalCacheApiOperation localCacheApiOperation;

    @ApiOperation(value = "刷新缓存(集群刷新)")
    @PostMapping("/refresh")
    public Result<Void> refresh(@RequestBody LocalCacheOptReqDto reqDto) {
        localCacheApiOperation.reload(reqDto);
        return Result.success("刷新缓存成功");
    }

    @ApiOperation(value = "API刷新缓存(单机刷新)", hidden = true)
    @PostMapping("/api/refresh")
    public Result<Void> apiRefresh(@RequestBody LocalCacheOptReqDto reqDto) {
        localCacheContext.reload(reqDto.getCacheType(), reqDto.getKey());
        return Result.success("API刷新缓存成功");
    }

    @ApiOperation(value = "清空缓存(集群清空)")
    @PostMapping("/clear")
    public Result<Void> clear(@RequestBody LocalCacheOptReqDto reqDto) {
        localCacheApiOperation.clear(reqDto);
        return Result.success("清空缓存成功");
    }

    @ApiOperation(value = "API清空缓存(单机清空)", hidden = true)
    @PostMapping("/api/clear")
    public Result<Void> apiClear(@RequestBody LocalCacheOptReqDto reqDto) {
        localCacheContext.clear(reqDto.getCacheType(), reqDto.getKey());
        return Result.success("API清空缓存成功");
    }
}
